#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
客户详情页面分组验证脚本
验证字段分组是否正确实现
"""

import re
import os

def verify_grouping():
    file_path = 'frontend/src/views/crm/components/CustomerDetailNew.vue'

    print("=" * 80)
    print("🔍 客户详情页面分组验证")
    print("=" * 80)

    # 读取文件
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # 验证项目
    checks = []

    # 1. 检查6个主要卡片是否存在
    cards = [
        ('基础信息', 'el-card.*header="基础信息"'),
        ('企业信息', 'el-card.*header="企业信息"'),
        ('证件信息', 'el-card.*header="证件信息"'),
        ('营销信息', 'el-card.*header="营销信息"'),
        ('地址信息', 'el-card.*header="地址信息"'),
        ('联系人信息表格', 'el-card.*header="联系人信息".*el-table'),
    ]

    for card_name, pattern in cards:
        if re.search(pattern, content, re.DOTALL):
            checks.append(("✅", f"存在【{card_name}】卡片"))
        else:
            checks.append(("❌", f"缺少【{card_name}】卡片"))

    # 2. 检查条件渲染
    conditional_checks = [
        ('企业信息卡片条件渲染', 'v-if="detail.customer_type === 2".*企业信息'),
        ('法定代表人条件渲染', 'v-if="detail.customer_type === 2".*法定代表人'),
        ('合作时间条件渲染', 'v-if="detail.is_cooperation".*合作时间'),
    ]

    for check_name, pattern in conditional_checks:
        if re.search(pattern, content, re.DOTALL):
            checks.append(("✅", f"正确实现【{check_name}】"))
        else:
            checks.append(("❌", f"缺少【{check_name}】"))

    # 3. 检查映射方法
    methods = [
        'getSourceText',
        'getIndustryText',
        'getScaleText',
        'getLevelText',
        'getImportantLevelText',
        'getIdTypeText',
        'getCustomerAttributeText',
    ]

    for method in methods:
        if f'const {method}' in content:
            checks.append(("✅", f"存在方法【{method}】"))
        else:
            checks.append(("❌", f"缺少方法【{method}】"))

    # 4. 检查重复联系人信息
    if '简化版联系人信息卡片' not in content and content.count('header="联系人信息"') == 1:
        checks.append(("✅", "已移除重复的联系人信息卡片"))
    else:
        checks.append(("❌", "仍存在重复的联系人信息"))

    # 5. 检查字段分布
    field_distribution = {
        '基础信息': ['客户名称', '联系电话', '负责人', '所属部门', '客户来源', '客户等级', '重要程度', '客户状态'],
        '企业信息': ['行业', '企业规模'],
        '证件信息': ['法定代表人', '证件类型', '证件号码', '客户属性'],
        '营销信息': ['合作状态', '合作时间'],
        '地址信息': ['完整地址'],
    }

    for card_name, fields in field_distribution.items():
        missing_fields = []
        for field in fields:
            # 检查字段是否在对应卡片中
            if card_name == '企业信息':
                if not (f'label="{field}"' in content and 'v-if="detail.customer_type === 2"' in content):
                    missing_fields.append(field)
            elif card_name == '证件信息' and field == '法定代表人':
                if not ('label="法定代表人"' in content and 'v-if="detail.customer_type === 2"' in content):
                    missing_fields.append(field)
            elif card_name == '营销信息' and field == '合作时间':
                if not ('label="合作时间"' in content and 'v-if="detail.is_cooperation"' in content):
                    missing_fields.append(field)
            else:
                if f'label="{field}"' not in content:
                    missing_fields.append(field)

        if not missing_fields:
            checks.append(("✅", f"【{card_name}】字段完整"))
        else:
            checks.append(("❌", f"【{card_name}】缺少字段：{', '.join(missing_fields)}"))

    # 输出验证结果
    print("\n📋 验证结果：\n")
    for status, message in checks:
        print(f"{status} {message}")

    # 统计
    passed = sum(1 for s, _ in checks if s == "✅")
    failed = sum(1 for s, _ in checks if s == "❌")
    total = len(checks)

    print("\n" + "=" * 80)
    print(f"📊 验证统计：{passed}/{total} 项通过，{failed} 项失败")
    print("=" * 80)

    if failed == 0:
        print("\n🎉 所有验证项目均通过！分组优化完成质量良好。")
    else:
        print(f"\n⚠️  有 {failed} 项验证失败，请检查代码实现。")

    return failed == 0

if __name__ == '__main__':
    verify_grouping()
